package com.zaker.touchtv.db;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.annotation.Size;

import com.zaker.touchtv.R;

/**
 * Created by Zonda on 2016/9/27.
 */

public class LiveDBHelper {

  private final static String SHARED_PREFERENCES_NAME = "LiveDBHelper";

  private final static String USER_UID_KEY = "user_uid_key";

  private final static String USER_NAME_KEY = "user_name_key";

  private final static String USER_TOKEN_KEY = "user_token_key";

  private final static String SELECTED_LIVE_DEVICE_KEY = "selected_live_device_key";

  public static void saveUserInfo(String uid, String username, String token,
                                  Context appContext) {

    SharedPreferences sharedPreferences = appContext.getSharedPreferences(SHARED_PREFERENCES_NAME,
        Context.MODE_PRIVATE);
    sharedPreferences.edit().putString(USER_UID_KEY, uid)
        .putString(USER_NAME_KEY, username)
        .putString(USER_TOKEN_KEY, token)
        .apply();
  }

  /**
   * 获取用户信息
   *
   * @param appContext 应用上下文
   * @return 返回一个大小为4的数组，按索引依次表示是否存在有效用户信息标识位、用户Id、用户名、用户Token
   */
  public static
  @Size(4)
  String[] getUserInfo(Context appContext) {

    SharedPreferences sharedPreferences = appContext.getSharedPreferences(SHARED_PREFERENCES_NAME,
        Context.MODE_PRIVATE);
    String[] userInfo = new String[4];

    userInfo[1] = sharedPreferences.getString(USER_UID_KEY, null);
    if (userInfo[1] == null) {
      userInfo[0] = null;
      return userInfo;
    }

    userInfo[2] = sharedPreferences.getString(USER_NAME_KEY, null);
    if (userInfo[2] == null) {
      userInfo[0] = null;
      return userInfo;
    }

    userInfo[3] = sharedPreferences.getString(USER_TOKEN_KEY, null);
    if (userInfo[2] == null) {
      userInfo[0] = null;
      return userInfo;
    }
    userInfo[0] = "Y";
    return userInfo;
  }

  public static boolean hasLogin(Context appContext) {
    String[] userInfo = getUserInfo(appContext);
    return userInfo[0] != null;
  }

  public static void clearUserInfo(Context appContext) {
    SharedPreferences sharedPreferences = appContext.getSharedPreferences(SHARED_PREFERENCES_NAME,
        Context.MODE_PRIVATE);
    sharedPreferences.edit().clear().apply();
  }

  public static void setSelectedDeviceIndex(int selectedIndex, Context appContext) {
    SharedPreferences sharedPreferences = appContext.getSharedPreferences(SHARED_PREFERENCES_NAME,
        Context.MODE_PRIVATE);
    sharedPreferences.edit().putInt(SELECTED_LIVE_DEVICE_KEY, selectedIndex).apply();
  }

  public static int getSelectedDeviceIndex(Context appContext) {
    SharedPreferences sharedPreferences = appContext.getSharedPreferences(SHARED_PREFERENCES_NAME,
        Context.MODE_PRIVATE);
    int selectedIndex = sharedPreferences.getInt(SELECTED_LIVE_DEVICE_KEY, 0);
    final String[] selectedFilmedDevices = appContext.getResources()
        .getStringArray(R.array.selected_filmed_device);
    selectedIndex = selectedIndex < selectedFilmedDevices.length ? selectedIndex : 0;
    return selectedIndex;
  }

  public static String getSelectedDeviceName(Context appContext) {
    SharedPreferences sharedPreferences = appContext.getSharedPreferences(SHARED_PREFERENCES_NAME,
        Context.MODE_PRIVATE);
    int selectedIndex = sharedPreferences.getInt(SELECTED_LIVE_DEVICE_KEY, 0);
    final String[] selectedFilmedDevices = appContext.getResources()
        .getStringArray(R.array.selected_filmed_device);
    selectedIndex = selectedIndex < selectedFilmedDevices.length ? selectedIndex : 0;
    return selectedFilmedDevices[selectedIndex];
  }
}
